# In this tutorial we will produce surface plots.
import numpy as np
import matplotlib.pyplot as plt
# As example, we will plot the surface given by x*exp(-x^2-y^2). To do
# this we have to make arrays of x values and y values.
x = np.arange(-2, 2, 0.35)
y = np.arange(-2, 2, 0.35)
n = len(x)
m = len(y)
n, m
(12, 12)
# We can then use 'np.meshgrid' to establish a list of all of the
# required (x, y) coordinates. I will plot the coordinates so that we can
# visualize the grid.
x, y = np.meshgrid(x, y)
plt.plot(x, y, marker='.', color='k', linestyle='none');
# We can now calculate the z-values at each of the grid positions.
z = x*np.exp(-x**2 - y**2)
z
array([[-0.00067093, -0.00198582, -0.00439347, -0.00705659, -0.00766703, -0.00430149, 0.00181334, 0.00673116, 0.00772616, 0.00561262, 0.00289568, 0.00110566], [-0.00240705, -0.00712444, -0.01576228, -0.02531664, -0.0275067 , -0.01543227, 0.00650564, 0.02414911, 0.02771882, 0.02013616, 0.01038872, 0.00396673], [-0.00675919, -0.02000597, -0.04426169, -0.07109109, -0.07724094, -0.04333501, 0.01826835, 0.06781257, 0.0778366 , 0.05654389, 0.02917232, 0.01113887], [-0.01485598, -0.04397101, -0.09728254, -0.15625073, -0.16976747, -0.0952458 , 0.04015192, 0.1490449 , 0.17107665, 0.12427753, 0.0641177 , 0.02448207], [-0.02555678, -0.07564343, -0.16735537, -0.26879849, -0.29205135, -0.16385156, 0.06907343, 0.25640227, 0.29430355, 0.21379492, 0.11030182, 0.04211656], [-0.0344119 , -0.101853 , -0.22534207, -0.36193404, -0.39324375, -0.22062423, 0.09300657, 0.34524268, 0.39627631, 0.28787237, 0.14852011, 0.05670945], [-0.03626679, -0.10734313, -0.23748858, -0.38144321, -0.4144406 , -0.23251644, 0.09801987, 0.36385214, 0.41763662, 0.30338943, 0.15652573, 0.05976624], [-0.02991627, -0.08854674, -0.19590298, -0.31465034, -0.34186969, -0.19180149, 0.08085603, 0.30013956, 0.34450608, 0.25026422, 0.12911718, 0.04930083], [-0.0193154 , -0.05717007, -0.12648447, -0.20315353, -0.22072766, -0.12383635, 0.05220458, 0.19378467, 0.22242984, 0.16158272, 0.08336432, 0.03183101], [-0.00976107, -0.02889101, -0.06391918, -0.10266405, -0.11154517, -0.06258095, 0.02638169, 0.09792948, 0.11240537, 0.08165616, 0.04212833, 0.01608586], [-0.00386091, -0.01142759, -0.02528268, -0.04060787, -0.04412073, -0.02475335, 0.01043505, 0.03873515, 0.04446097, 0.03229838, 0.01666349, 0.00636262], [-0.00119531, -0.00353789, -0.00782732, -0.01257187, -0.01365942, -0.00766344, 0.00323061, 0.01199209, 0.01376476, 0.00999932, 0.00515888, 0.00196982]])
# Here's a contour plot of the data.
plt.contourf(x, y, z);
# Here's a surface plot. The syntax is a little different than we've been using
# in the previous tutorials. Notice that I've specified the size of the figure
# in plt.figure(). 'cmap' stands for colormap and there are many options to
# choose from.
plt.figure(figsize=(15,15))
ax = plt.axes(projection='3d')
ax.plot_surface(x, y, z,cmap='viridis')
ax.set_title('Surface plot');
# The figures above are not smooth because the data was too coarse. Here, we
# reproduce the figures using a more finely-spaced grid of (x, y) coordinates.
x = np.arange(-2, 2, 0.01)
y = np.arange(-2, 2, 0.01)
n = len(x)
m = len(y)
x, y = np.meshgrid(x, y)
plt.figure(figsize=(15,15))
plt.plot(x,y, marker='.', markersize = 1, color='k', linestyle='none');
# We can now calculate the z-values at each of the grid positions.
z = x*np.exp(-x**2 - y**2)
# Here's a contour plot of the data.
plt.contourf(x, y, z);
# Here's a surface plot. I changed the orientation of the figure using 'azim'
# and 'elev'. For fun, I also changed the 'cmap' option and made the surface
# slightly transparent using `alpha'. I also specified the number of grid lines
# that appear on the surface use 'rcount' (row count) and 'ccount' (column count).
# Finally, I lablled the x-, y-, and z-axes.
fig = plt.figure(figsize=(15,15))
ax = plt.axes(projection='3d')
ax.view_init(azim=30)
ax.view_init(elev=15)
ax.plot_surface(x, y, z,cmap='seismic', alpha = 0.75, linewidth = 1,\
edgecolors = 'k', rcount = 30, ccount = 30)
ax.set_title('Surface plot')
ax.set_xlabel('x axis')
ax.set_ylabel('y axis')
ax.set_zlabel('z axis');